#include "chessdata.h"
#include "qDebug"



ChessData::ChessData() {

    for(int i=0;i<9;i++){
        for(int k=0;k<10;k++){
            boardTable[i][k]=nullptr;
        }
    }

     canMovePos=new QList<int*>();

}



ChessData::~ChessData()
{
    if(deadPieces!=nullptr){
        deleteDeadPieces();
    }
    if(pieceHash!=nullptr){
        //qDeleteAll(pieceHash);
        pieceHash->clear();
    }
    for(int i=0;i<9;i++){
        for(int k=0;k<10;k++){
            if(boardTable[i][k]!=nullptr){
                delete boardTable[i][k];
            }
        }
    }
    if(canMovePos!=nullptr){
        qDeleteAll(*canMovePos);
        //canMovePos->clear();
        delete canMovePos;
    }
    delete sendBus;
}

void ChessData::resetGame(bool isRed)
{


    //步数清零
    step=0;
    //sideStep=isRed?1:0;
    if(sideBelow==1){
        sideStep=0;
    }else{
        sideStep=1;
    }

    //棋子清零
    if(deadPieces!=nullptr){
        deleteDeadPieces();
    }


    for(int i=0;i<9;i++){
        for(int k=0;k<10;k++){
            if(boardTable[i][k]!=nullptr){
                delete boardTable[i][k];
            }
        }
    }



    //重新生成新棋子
    pieceHash=new QHash<QString,PieceData>();
    for(int i=0;i<32;i++){
        int type=i<=15?0:1;
        PieceData* pd=new PieceData(i+1,pnames[i],type);
        if(this->sideBelow==0){
            int col=redPos[i][0];
            int row=redPos[i][1];
            pd->setPos(col,row);
            pd->setKind(kinds[i]);
            boardTable[col][row]=pd;
        }else{
            int col=this->blackPos[i][0];
            int row=this->blackPos[i][1];
            pd->setPos(col,row);
            pd->setKind(kinds[i]);
            boardTable[col][row]=pd;
        }
    }
    //printTableBoard();

}

void ChessData::setSpos(double sx, double sy)
{
    this->sx=sx;
    this->sy=sy;
}

void ChessData::setRd(double r, double d)
{
    this->r=r;
    this->d=d;
}

void ChessData::saveSelectPieceInfo(int id, int col, int row,int side,int type)
{
    this->selectId=id;
    this->col=col;
    this->row=row;
    this->selectSide=side;
    this->selType=type;
}

void ChessData::cancelSelectPiece()
{
    this->selectId=-1;
    this->col=-1;
    this->row=-1;
}

void ChessData::printTableBoard()
{
    for(int i=0;i<9;i++){
        for(int k=0;k<10;k++){
            if(boardTable[i][k]!=nullptr){
                PieceData* ped=boardTable[i][k];
                 qDebug()<<"boardTable:"<<"i_k="<<i<<"_"<<k<<":"<<ped->textName<<"||"<<ped->cx<<"_"<<ped->cy;
            }else{
                 qDebug()<<"boardTable: no data:"<<i<<"_"<<k;
            }
        }
    }
}

void ChessData::saveMoveInfo(int oldCol, int oldRow, int col, int row)
{
   PieceData* cd= this->boardTable[oldCol][oldRow];
   cd->setPos(col,row);
   boardTable[oldCol][oldRow]=nullptr;
   if(boardTable[col][row]!=nullptr){
       delete boardTable[col][row];
   }
   boardTable[col][row]=cd;
   saveStepCount();
}


// 计算相的移动范围




 //棋子种类: 1 车  2 马 3 炮 4 相 5 士 6 将 7卒
void ChessData::calcCanMovePos()
{
    if(selectId!=-1){
        if(canMovePos!=nullptr){
            qDeleteAll(*canMovePos);
            canMovePos->clear();
            delete canMovePos;
        }
        canMovePos =new QList<int*>();
        //有棋子被选中

        qDebug()<<"calcMovePos:"<<this->selType<<":"<<this->selectId;

        switch(this->selType){
        case 1:
        case 3:
            //车,炮 1 上  2 下  3 左 4 右
            {
                //下移
                for(int i=this->row+1;i<=ChessData::max_row;i++){
                    if(this->boardTable[this->col][i]==nullptr){
                        canMovePos->append(new int[]{this->col,i});
                    }else{
                        break;
                    }
                }
                //上移
                for(int i=this->row-1;i>=0;i--){
                    if(this->boardTable[this->col][i]==nullptr){
                        canMovePos->append(new int[]{this->col,i});
                    }else{
                        break;
                    }
                }
                //左移
                for(int i=this->col-1;i>=0;i--){
                    if(this->boardTable[i][this->row]==nullptr){
                        canMovePos->append(new int[]{i,this->row});
                    }else{
                        break;
                    }
                }
                //右移
                for(int i=this->col+1;i<=ChessData::max_col;i++){
                    if(this->boardTable[i][this->row]==nullptr){
                        canMovePos->append(new int[]{i,this->row});
                    }else{
                        break;
                    }
                }
                break;
            }
        case 2:
            //马 计算8个点位
            {
                //右上,右中上,右中下,右下,左上,左中上,左中下,左下
                if(this->row-2>=0 && this->col+1<=ChessData::max_col &&  this->boardTable[this->col][this->row-1]==nullptr
                    && this->boardTable[this->col+1][this->row-2]==nullptr){
                    canMovePos->append(new int[]{this->col+1,this->row-2});
                }
                if(this->col+2<=ChessData::max_col && this->row-1 >=0 && this->boardTable[this->col+1][this->row]==nullptr
                    && this->boardTable[this->col+2][this->row-1]==nullptr){
                    canMovePos->append(new int[]{this->col+2,this->row-1});
                }
                if(this->col+2<=ChessData::max_col && this->row+1 <=ChessData::max_row && this->boardTable[this->col+1][this->row]==nullptr
                    && this->boardTable[this->col+2][this->row+1]==nullptr){
                    canMovePos->append(new int[]{this->col+2,this->row+1});
                }
                if(this->col+1<=ChessData::max_col && this->row+2 <=ChessData::max_row && this->boardTable[this->col][this->row+1]==nullptr
                    && this->boardTable[this->col+1][this->row+2]==nullptr){
                    canMovePos->append(new int[]{this->col+1,this->row+2});
                }
                if(this->col-1>=0 && this->row+2 <=ChessData::max_row && this->boardTable[this->col][this->row+1]==nullptr
                    && this->boardTable[this->col-1][this->row+2]==nullptr){
                    canMovePos->append(new int[]{this->col-1,this->row+2});
                }
                if(this->col-2>=0 && this->row+1<=ChessData::max_row && this->boardTable[this->col-1][this->row]==nullptr
                    && this->boardTable[this->col-2][this->row+1]==nullptr){
                    canMovePos->append(new int[]{this->col-2,this->row+1});
                }
                if(this->col-2>=0 && this->row-1>=0 && this->boardTable[this->col-1][this->row]==nullptr
                    && this->boardTable[this->col-2][this->row-1]==nullptr){
                    canMovePos->append(new int[]{this->col-2,this->row-1});
                }
                if(this->col-1>=0 && this->row-2>=0 && this->boardTable[this->col][this->row-1]==nullptr
                    && this->boardTable[this->col-1][this->row-2]==nullptr){
                    canMovePos->append(new int[]{this->col-1,this->row-2});
                }

               break;
            }
        case 4:
            if(this->sideBelow==0){
                //红色棋子在下方
                if(this->selectSide==1){
                    //红色相
                    this->calcElephantPos(canMovePos,5,ChessData::max_row);
                }else{
                    //黑色相
                    this->calcElephantPos(canMovePos,0,4);
                }
            }else{
                //黑色棋子在下方
                if(this->selectSide==1){
                    //红色相
                    this->calcElephantPos(canMovePos,0,4);
                }else{
                    //黑色相
                    this->calcElephantPos(canMovePos,5,ChessData::max_row);
                }
            }
            break;
        case 5:
            if(this->sideBelow==0){
                //红色棋子在下方
                if(this->selectSide==1){
                    this->calcAdvisorPos(canMovePos,7,9,3,5);
                }else{
                    this->calcAdvisorPos(canMovePos,0,2,3,5);
                }
            }else{
                if(this->selectSide==1){
                    this->calcAdvisorPos(canMovePos,0,2,3,5);
                }else{
                    this->calcAdvisorPos(canMovePos,7,9,3,5);
                }
            }
            break;
        case 6:
            if(this->sideBelow==0){
                if(this->selectSide==1){
                    this->calcKingPos(canMovePos,7,9,3,5);
                }else{
                    this->calcKingPos(canMovePos,0,2,3,5);
                }
            }else{
                if(this->selectSide==1){
                    this->calcKingPos(canMovePos,0,2,3,5);
                }else{
                    this->calcKingPos(canMovePos,7,9,3,5);
                }
            }
            break;
        case 7:
            if(this->sideBelow==0){
                //红色棋子在下方
                if(this->selectSide==1){
                    //红色
                    this->calcPawnPos(canMovePos,5,1);
                }else{
                    this->calcPawnPos(canMovePos,4,0);
                }
            }else{
                //黑色棋子在下方
                if(this->selectSide==1){
                    //红色
                    this->calcPawnPos(canMovePos,4,0);
                }else{
                    this->calcPawnPos(canMovePos,5,1);
                }
            }
            break;
        }

        for(auto item: *canMovePos){
            qDebug()<<"movePos:="<<item[0]<<"_"<<item[1];
        }
    }
}


 //计算该点有无车,马,炮,过河兵 杀
bool ChessData::calcPosCankill(int pcol,int prow,int side)
{
    //车,炮杀
    //上
    int n=0;
    for(int i=this->row-1;i>=0;i--){
        PieceData* pd = this->boardTable[this->col][i];
        if(pd!=nullptr && n==0 && pd->kind==1 && pd->type!=this->selectSide){
            //有不同颜色的车杀
            return true;
        }
        if(pd!=nullptr && n==1 && pd->kind==3 && pd->type!=this->selectSide){
            //有不同颜色的炮杀
            return true;
        }
        if(pd!=nullptr){
            n++;
            if(n>=2){
                break;
            }
        }
    }

    n=0;
    for(int i=this->row+1; i<=ChessData::max_row; i++){
        PieceData* pd = this->boardTable[this->col][i];
        if(pd!=nullptr && n==0 && pd->kind==1 && pd->type!=this->selectSide){
            //有不同颜色的车杀
            return true;
        }
        if(pd!=nullptr && n==1 && pd->kind==3 && pd->type!=this->selectSide){
            //有不同颜色的炮杀
            return true;
        }
        if(pd!=nullptr){
            n++;
            if(n>=2){
                break;
            }
        }
    }
    n=0;
    for(int i=this->col+1; i<=ChessData::max_col; i++){
        PieceData* pd = this->boardTable[i][this->row];
        if(pd!=nullptr && n==0 && pd->kind==1 && pd->type!=this->selectSide){
            //有不同颜色的车杀
            return true;
        }
        if(pd!=nullptr && n==1 && pd->kind==3 && pd->type!=this->selectSide){
            //有不同颜色的炮杀
            return true;
        }
        if(pd!=nullptr){
            n++;
            if(n>=2){
                break;
            }
        }
    }
    n=0;
    for(int i=this->col-1; i>=0; i++){
        PieceData* pd = this->boardTable[i][this->row];
        if(pd!=nullptr && n==0 && pd->kind==1 && pd->type!=this->selectSide){
            //有不同颜色的车杀
            return true;
        }
        if(pd!=nullptr && n==1 && pd->kind==3 && pd->type!=this->selectSide){
            //有不同颜色的炮杀
            return true;
        }
        if(pd!=nullptr){
            n++;
            if(n>=2){
                break;
            }
        }
    }
    //马杀
    if(this->row-2>=0 && this->row-1>=0 && this->col+1<=ChessData::max_col &&  this->boardTable[this->col][this->row-1]==nullptr
        ){
        PieceData* pd =this->boardTable[this->col+1][this->row-2];
        if(pd->kind==2 && pd->type!=this->selectSide){
            return true;
        }
    }
    if(this->col+2<=ChessData::max_col && this->col+1 <=ChessData::max_col && this->row-1 >=0 && this->boardTable[this->col+1][this->row]==nullptr
       ){

        PieceData* pd =this->boardTable[this->col+2][this->row-1];
        if(pd->kind==2 && pd->type!=this->selectSide){
            return true;
        }

    }
    if(this->col+2<=ChessData::max_col && this->col+1<=ChessData::max_col && this->row+1 <=ChessData::max_row && this->boardTable[this->col+1][this->row]==nullptr
        ){

        PieceData* pd =this->boardTable[this->col+2][this->row+1];
        if(pd->kind==2 && pd->type!=this->selectSide){
            return true;
        }

    }
    if(this->col+1<=ChessData::max_col && this->row+1 <=ChessData::max_row && this->row+2 <=ChessData::max_row && this->boardTable[this->col][this->row+1]==nullptr
        ){
        PieceData* pd =this->boardTable[this->col+1][this->row+2];
        if(pd->kind==2 && pd->type!=this->selectSide){
            return true;
        }
    }
    if(this->col-1>=0 && this->row+1 <=ChessData::max_row  &&this->row+2 <=ChessData::max_row && this->boardTable[this->col][this->row+1]==nullptr
        ){

        PieceData* pd =this->boardTable[this->col-1][this->row+2];
        if(pd->kind==2 && pd->type!=this->selectSide){
            return true;
        }

    }
    if(this->col-2>=0 && this->col-1>=0 && this->row+1<=ChessData::max_row && this->boardTable[this->col-1][this->row]==nullptr
        ){

        PieceData* pd =this->boardTable[this->col-2][this->row+1];
        if(pd->kind==2 && pd->type!=this->selectSide){
            return true;
        }
    }
    if(this->col-2>=0 && this->col-1>0 && this->row-1>=0 && this->boardTable[this->col-1][this->row]==nullptr
        ){

        PieceData* pd =this->boardTable[this->col-2][this->row-1];
        if(pd->kind==2 && pd->type!=this->selectSide){
            return true;
        }
    }
    if(this->col-1>=0 && this->row-1>=0 && this->row-2>=0 && this->boardTable[this->col][this->row-1]==nullptr
       ){
        PieceData* pd =this->boardTable[this->col-1][this->row-2];
        if(pd->kind==2 && pd->type!=this->selectSide){
            return true;
        }
    }
    //已过河兵杀 0 红色棋子在下方  1 黑色棋子在下方
    if(this->row>=5){
        //下方过河兵
        PieceData* pd =this->boardTable[this->col-1][this->row];
        if(pd!=nullptr && pd->kind==7 && pd->type!=this->selectSide){
            return true;
        }
        pd=this->boardTable[this->col+1][this->row];
        if(pd!=nullptr && pd->kind==7 && pd->type !=this->selectSide){
            return true;
        }
        pd=this->boardTable[this->col][this->row-1];
        if(pd!=nullptr && pd->kind==7 && pd->type !=this->selectSide){
            return true;
        }
    }else if(this->row<=4){
        //上方过河兵
        PieceData* pd =this->boardTable[this->col-1][this->row];
        if(pd!=nullptr && pd->kind==7 && pd->type!=this->selectSide){
            return true;
        }
        pd=this->boardTable[this->col+1][this->row];
        if(pd!=nullptr && pd->kind==7 && pd->type !=this->selectSide){
            return true;
        }
        pd=this->boardTable[this->col][this->row+1];
        if(pd!=nullptr && pd->kind==7 && pd->type !=this->selectSide){
            return true;
        }
    }
    return false;
}

//判断是否可以吃掉对方棋子
bool ChessData::canEatPiece(int desCol, int desRow){
    if(this->selectId==-1){
        return false;
    }
    PieceData* desPd=this->boardTable[desCol][desRow];
    if(desPd==nullptr){
        return false;
    }
    if(desPd!=nullptr&&desPd->type==this->selectSide){
        //同颜色棋子,不可移动
        return false;
    }
    return canMoveToPos(desCol,desRow);
}


// 1 车  2 马 3 炮 4 相 5 士 6 将 7卒
bool ChessData::canMoveToPos(int desCol, int desRow)
{
    if(this->selectId==-1){
        return false;
    }

    PieceData* desPd=this->boardTable[desCol][desRow];

    if(desPd!=nullptr&&desPd->type==this->selectSide){
        //同颜色棋子,不可移动
        return false;
    }

    switch (this->selType) {
        case 1:{
                //车,同直线,且无间隔棋子
                if(this->col==desCol){
                    int k= desRow>this->row ? 1:-1;
                    int startNum=this->row+k;
                    do{
                        if(startNum!=desRow && this->boardTable[this->col][startNum]!=nullptr){
                            return false;
                        }
                        if(startNum==desRow){
                            return true;
                        }
                        startNum=startNum+k;
                    }while(true);

                }else if(this->row==desRow){
                    int k= desCol>this->col ? 1:-1;
                    int startNum=this->col+k;
                    do{
                        if(startNum!=desCol && this->boardTable[startNum][this->row]!=nullptr){
                            return false;
                        }
                        if(startNum==desCol){
                            return true;
                        }
                        startNum=startNum+k;
                    }while(true);

                }else{
                    return false;
                }
                break;
            }
        case 2:{
               //马
                int diffCol=qAbs(this->col-desCol);
                int diffRow=qAbs(this->row-desRow);
                if(diffCol==1){
                    if(this->row-desRow==2 && this->boardTable[this->col][this->row-1]==nullptr){
                        return true;
                    }else if(this->row-desRow==-2 && this->boardTable[this->col][this->row+1]==nullptr){
                        return true;
                    }else{
                        return false;
                    }
                }else if(diffRow==1){
                    if(this->col-desCol==2 && this->boardTable[this->col-1][this->row]==nullptr){
                        return true;
                    }else if(this->col-desCol== -2 && this->boardTable[this->col+1][this->row]==nullptr ){
                        return true;
                    }else{
                        return false;
                    }
                }
                break;
            }
        case 3:{

                if(desPd!=nullptr && desPd->type != this->selectSide){
                     //有不同色棋子,判断是否可以吃掉
                    if(this->col==desCol){
                         int k= desRow>this->row ? 1:-1;
                         int startNum=this->row+k;
                         int gapone=0;
                         do{
                             if(startNum!=desRow && this->boardTable[this->col][startNum]!=nullptr){
                                 gapone++;
                                 if(gapone>1){
                                     return false;
                                 }
                             }
                             if(startNum==desRow){
                                 break;
                             }
                             startNum=startNum+k;
                         }while(true);
                         if(gapone==1){
                             return true;
                         }
                    }else if(this->row==desRow){
                        int k= desCol>this->col ? 1:-1;
                        int startNum=this->col+k;
                        int gapone=0;
                        do{
                            if(startNum!=desCol && this->boardTable[startNum][this->row]!=nullptr){
                                gapone++;
                                if(gapone>1){
                                    return false;
                                }
                            }
                            if(startNum==desCol){
                                 break;
                            }
                            startNum=startNum+k;
                        }while(true);
                        if(gapone==1){
                            return true;
                        }
                    }
                }else if(desPd==nullptr){
                    //空位置,判断是否可以移动
                    if(this->col==desCol){
                        int k= desRow>this->row ? 1:-1;
                        int startNum=this->row+k;
                        do{
                            if(startNum!=desRow && this->boardTable[this->col][startNum]!=nullptr){
                                return false;
                            }
                            if(startNum==desRow){
                                return true;
                            }
                            startNum=startNum+k;
                        }while(true);

                    }else if(this->row==desRow){
                        int k= desCol>this->col ? 1:-1;
                        int startNum=this->col+k;
                        do{
                            if(startNum!=desCol && this->boardTable[startNum][this->row]!=nullptr){
                                return false;
                            }
                            if(startNum==desCol){
                                return true;
                            }
                            startNum=startNum+k;
                        }while(true);
                    }
                }
            }
            break;

        case 4:{
                if((this->sideBelow==0 && this->selectSide==1) || (this->sideBelow==1 && this->selectSide==0) ){
                    //红色棋子在下方,红色走起,黑色棋子在下方,黑色走起
                    if(desRow <5)
                        return false;
                }else if((this->sideBelow==1 && this->selectSide==1) || (this->sideBelow==0 || this->selectSide==0)){
                    //上方走起
                    if(desRow>4)
                        return false;
                }


                int diffCol=desCol-this->col;
                int diffRow=desRow-this->row;
                if(diffCol==2 && diffRow==2 && this->boardTable[this->col+1][this->row+1] == nullptr ){
                    return true;
                }else if(diffCol==2 && diffRow==-2 && this->boardTable[this->col+1][this->row-1] == nullptr ){
                    return true;
                }else if(diffCol==-2 && diffRow==-2 && this->boardTable[this->col-1][this->row-1] == nullptr ){
                    return true;
                }else if(diffCol==-2 && diffRow==2 && this->boardTable[this->col-1][this->row+1] == nullptr ){
                    return true;
                }
                break;
            }

        case 5:{
                int diffCol=desCol-this->col;
                int diffRow=desRow-this->row;
                int mincol,maxcol,minrow,maxrow=0;
                if((this->sideBelow==0 && this->selectSide==1) || (this->sideBelow==1 && this->selectSide==0) ){
                    //红色棋子在下方,红色走起,黑色棋子在下方,黑色走起
                    minrow=7;maxrow=9;
                    mincol=3;maxcol=5;
                }else if((this->sideBelow==1 && this->selectSide==1) || (this->sideBelow==0 || this->selectSide==0)){
                    //上方走起
                    minrow=0;maxrow=2;
                    mincol=3;maxcol=5;
                }else{
                    return false;
                }
                if(qAbs(diffCol)==1 && qAbs(diffRow) && (desCol<=maxcol && desCol>=mincol) && (desRow<=maxrow && desRow >= minrow)){
                    return true;
                }else{
                    return false;
                }
            }

        case 6:{
                int diffCol=desCol-this->col;
                int diffRow=desRow-this->row;
                int mincol,maxcol,minrow,maxrow=0;
                if((this->sideBelow==0 && this->selectSide==1) || (this->sideBelow==1 && this->selectSide==0) ){
                    //红色棋子在下方,红色走起,黑色棋子在下方,黑色走起
                    minrow=7;maxrow=9;
                    mincol=3;maxcol=5;
                }else if((this->sideBelow==1 && this->selectSide==1) || (this->sideBelow==0 || this->selectSide==0)){
                    //上方走起
                    minrow=0;maxrow=2;
                    mincol=3;maxcol=5;
                }else{
                    return false;
                }
                bool inRage=(desCol<=maxcol && desCol>=mincol) && (desRow<=maxrow && desRow >= minrow);
                if( inRage && ( (diffCol==0 && qAbs(diffRow)==1) || (diffRow==0 && qAbs(diffCol)==1) ) ){
                    return true;
                }
                break;
            }
        case 7:{
                if((this->sideBelow==0 && this->selectSide==1) || (this->sideBelow==1 && this->selectSide==0) ){
                    //红色棋子在下方,红色走起,黑色棋子在下方,黑色走起
                    if(this->col==desCol && desRow - this->row==-1 && desRow>=0){
                        return true;
                    }
                    if(this->row<5 && this->row==desRow && qAbs(this->col-desCol)==1 && desCol>=0 && desCol <= ChessData::max_col ){
                        //已过河
                        return true;
                    }

                }else if((this->sideBelow==1 && this->selectSide==1) || (this->sideBelow==0 || this->selectSide==0)){
                    //上方走起
                    if(this->col==desCol && desRow - this->row==1 && desRow<=ChessData::max_row){
                        return true;
                    }

                    if(this->row>4 && this->row==desRow && qAbs(this->col-desCol)==1 && desCol>=0 && desCol <= ChessData::max_col ){
                        //已过河
                        return true;
                    }
                }else{
                    return false;
                }
            }
    }



    return false;
}

void ChessData::saveStepCount()
{
    step++;
    sideStep=(sideStep==0?1:0);
    emit this->sendBus->sendSideChange(sideStep,sideBelow);

}

void ChessData::setGameStart(int value)
{
    this->gameStart=value;
    qDebug()<<"gameStart changed:"<<value;
    this->resetGame(true);
    emit this->sendBus->sendGameStartToPainter(true);
    emit this->sendBus->sendViewUpdate();
}

void ChessData::setGameBelow(int value)
{
    this->sideBelow=value;
    qDebug()<<"sideBelow changed:"<<value;
}


//计算象
void ChessData::calcElephantPos(QList<int *> *res, int minlimtRow, int maxlimitRow){

    //右上
    if( this->col+1<=ChessData::max_col && this->row-1>=0
        &&this->col+2<=ChessData::max_col && this->row-2>=0
        &&this->boardTable[this->col+1][this->row-1]==nullptr
        &&this->boardTable[this->col+2][this->row-2]==nullptr
        &&this->row-2>=minlimtRow){
        res->append(new int[]{this->col+2,this->row-2});
    }
    //右下
    if( this->col+1<=ChessData::max_col && this->row+1<=ChessData::max_row
        &&this->col+2<=ChessData::max_col && this->row+2<=ChessData::max_row
        &&this->boardTable[this->col+1][this->row+1]==nullptr
        &&this->boardTable[this->col+2][this->row+2]==nullptr
        &&this->row+2<=maxlimitRow){
        res->append(new int[]{this->col+2,this->row+2});
    }
    //左上
    if( this->col-1>=0 && this->row-1>=0
        &&this->col-2>=0 && this->row-2>=0
        &&this->boardTable[this->col-1][this->row-1]==nullptr
        &&this->boardTable[this->col-2][this->row-2]==nullptr
        &&this->row-2>=minlimtRow){
        res->append(new int[]{this->col-2,this->row-2});
    }
    //左下
    if( this->col-1>=0 && this->row+1<=ChessData::max_row
        &&this->col-2>=0 && this->row+2<=ChessData::max_row
        &&this->boardTable[this->col-1][this->row+1]==nullptr
        &&this->boardTable[this->col-2][this->row+2]==nullptr
        &&this->row+2<=maxlimitRow){
        res->append(new int[]{this->col-2,this->row+2});
    }
}

//计算士
void ChessData::calcAdvisorPos(QList<int *> *res, int minlimitrow, int maxlimitrow, int minlimitcol, int maxlimitcol)
{
    //左上
    if( this->col-1>=minlimitcol && this->row-1>=minlimitrow
        && this->boardTable[this->col-1][this->row-1]==nullptr){
        res->append(new int[]{this->col-1,this->row-1});
    }
    //右上
    if( this->col+1<=maxlimitcol && this->row-1>=minlimitrow
        && this->boardTable[this->col+1][this->row-1]==nullptr){
        res->append(new int[]{this->col+1,this->row-1});
    }
    //右下
    if( this->col+1<=maxlimitcol && this->row+1<=maxlimitrow
        && this->boardTable[this->col+1][this->row+1]==nullptr){
        res->append(new int[]{this->col+1,this->row+1});
    }
    //左下
    if( this->col-1>=minlimitcol && this->row+1<=maxlimitrow
        && this->boardTable[this->col-1][this->row+1]==nullptr){
        res->append(new int[]{this->col-1,this->row+1});
    }
}

void ChessData::calcKingPos(QList<int *> *res, int minlimitrow, int maxlimitrow, int minlimitcol, int maxlimitcol)
{
    //上
    if(this->row-1>=minlimitrow && this->boardTable[this->col][this->row-1]==nullptr){
          res->append(new int[]{this->col,this->row-1});
    }
    //下
    if(this->row+1<=maxlimitrow && this->boardTable[this->col][this->row+1]==nullptr){
        res->append(new int[]{this->col,this->row+1});
    }
    //左
    if(this->col-1>=minlimitcol && this->boardTable[this->col-1][this->row]==nullptr){
        res->append(new int[]{this->col-1,this->row});
    }
    //右
    if(this->col+1<=maxlimitcol && this->boardTable[this->col+1][this->row]==nullptr){
        res->append(new int[]{this->col+1,this->row});
    }
}


  //side=1 上走 side=0 下走
void ChessData::calcPawnPos(QList<int *> *res, int midRow, int side)
{
    if(side==1){
        //上
        if(this->row<midRow){
            // 上,左,右
            if(this->row-1>=0 && this->boardTable[this->col][this->row-1]==nullptr){
                res->append(new int[]{this->col,this->row-1});
            }
            if(this->col-1>=0 && this->boardTable[this->col-1][this->row]==nullptr){
                res->append(new int[]{this->col-1,this->row});
            }
            if(this->col+1<=ChessData::max_col && this->boardTable[this->col+1][this->row]==nullptr){
                res->append(new int[]{this->col+1,this->row});
            }
        }else{
            //上
            if(this->row-1>=0 && this->boardTable[this->col][this->row-1]==nullptr){
                res->append(new int[]{this->col,this->row-1});
            }
        }
    }else{
        if(this->row>midRow){
            // 下,左,右
            if(this->row+1<=ChessData::max_row && this->boardTable[this->col][this->row+1]==nullptr){
                res->append(new int[]{this->col,this->row+1});
            }
            if(this->col-1>=0 && this->boardTable[this->col-1][this->row]==nullptr){
                res->append(new int[]{this->col-1,this->row});
            }
            if(this->col+1<=ChessData::max_col && this->boardTable[this->col+1][this->row]==nullptr){
                res->append(new int[]{this->col+1,this->row});
            }
        }else{
            //下
            if(this->row+1<=ChessData::max_row && this->boardTable[this->col][this->row+1]==nullptr){
                res->append(new int[]{this->col,this->row+1});
            }
        }
    }
}

void ChessData::deleteDeadPieces()
{
    if(deadPieces!=nullptr){
        qDebug()<<"dddhere";
        //qDeleteAll(deadPieces);
        //deadPieces->clear();
        deadPieces->clear();
    }
}



PieceData::PieceData(const PieceData &pd)
{
    this->id=pd.id;
    this->cx=pd.cx;
    this->cy=pd.cy;
    this->textName=pd.textName;
    this->type=pd.type;


}

PieceData::PieceData()
{

}

PieceData::PieceData(int id, QString name, int type)
{
    this->id=id;
    this->textName=name;
    this->type=type;
}

void PieceData::setPos(double cx, double cy)
{
    this->cx=cx;
    this->cy=cy;
}

void PieceData::setKind(int kind)
{
    this->kind=kind;
}



PieceData::~PieceData()
{

}

bool PieceData::operator==(const PieceData &pd)
{
    return this->id==pd.id;
}
